##################################################################
#
# interaction.R 
# 
# Lame-duck interaction plot
#
# 6/24/2016
#
##################################################################

rm(list=ls())

setwd("~/Dropbox/2016_06/lame_duck")

library(ggplot2)
library(ggExtra)
library(gridExtra)
library(interplot)
library(foreign)
library(readstata13)

# using readstata13 package to read the data formatted as stata
# dat <- read.dta13("17th NA-Selected Bills.dta")
# str(dat)

data1 <- read.csv("lame_duck_17.csv", header=TRUE, stringsAsFactors=FALSE)
str(data1)

data2 <- read.csv("lame_duck_18.csv", header=TRUE, stringsAsFactors = FALSE)
str(data2)

#######################################################################
# Voting Yea Models -with ideological difference from Floor Median
# Lame Duck multiplied by Ideological Drift from the floor median 
# Voting Yea Models -with ideological difference from Floor Median
# Lame Duck multiplied by Ideological Drift from the floor median 
# With Control Variables 
#######################################################################

# logit Model for Legislation 13 voting Yea only
data1$return18 <- ifelse(data1$X_18th==0,"Leave",data1$X_18th)
data1$return18 <- ifelse(data1$X_18th=="1","Return",data1$return18)
data1$return18 <- factor(data1$return18)
data1$X_dum2 <- factor(data1$X_dum2)

fit.1a <- glm(leg13_yeav ~ return18 + seniority + ideo_1D + ideo_d1 + 
               return18:ideo_d1 + X_dum2, data=data1, 
             family=binomial(link="logit"))
summary(fit.1a)

fit.1b <- glm(leg13_yeav ~ return18 + seniority + ideo_1D + ideo_d1 + 
                ideoshirk + X_dum2, data=data1, 
              family=binomial(link="logit"))
summary(fit.1b)

# logit model for Legislation 15 voting Yea only

fit.2a <- glm(leg16_yeav ~ X_18th + seniority + ideo_d1 + X_18th:ideo_d1 + X_dum2 + 
               soo_do_kwon, data = data1, family=binomial(link="logit"))

summary(fit.2a)

fit.2b <- glm(leg16_yeav ~ X_18th + seniority + ideo_d1 + ideoshirk + X_dum2 + 
               soo_do_kwon, data = data1, family=binomial(link="logit"))

summary(fit.2b)

# logit model for Legislation 27 voting Yea only
fit.3a <- glm(leg27_yeav ~ X_18th + seniority + ideo_d1 + ideo_d1:X_18th +
               X_dum2 + dj_chnam, data = data1,
             family=binomial(link="logit"))
summary(fit.3a)

fit.3b <- glm(leg27_yeav ~ X_18th + seniority + ideo_d1 + ideoshirk +
               X_dum2 + dj_chnam, data = data1,
             family=binomial(link="logit"))
summary(fit.3b)

# logit model for Legislation 28 voting Yea only
fit.4a <- glm(leg28_yeav ~ X_18th + seniority + ideo_d1 + X_18th:ideo_d1 +
               X_dum2 + dj_chnam + jeonnam, data = data1, 
             family=binomial(link="logit"))
summary(fit.4a)

fit.4b <- glm(leg28_yeav ~ X_18th + seniority + ideo_d1 + ideoshirk +
               X_dum2 + dj_chnam + jeonnam, data = data1, 
             family=binomial(link="logit"))
summary(fit.4b)

# creating a basic graph of the conditional coefficients
interplot(m = fit.1a, var1 = "return18", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("18대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

interplot(m = fit.2a, var1 = "X_18th", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("18대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

interplot(m = fit.3a, var1 = "X_18th", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("18대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

interplot(m = fit.4a, var1 = "X_18th", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("18대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

#####################################################################
#####################################################################
# 18대
#####################################################################
#####################################################################

# logit Model for Legislation 13 voting Yea only
data2$return19 <- ifelse(data2$X_19th==0,"Leave",data2$X_19th)
data2$return19 <- ifelse(data2$X_19th=="1","Return",data2$return19)
data2$return19 <- factor(data2$return19)
data2$X_dum2 <- factor(data2$X_dum2)

# 3-1) For Significant Bills: L2, L15, L37
# Voting Yea Models -with ideological difference from Floor Median
# Lame Duck multiplied by Ideological Drift from the floor median 
# Without Control Variables 

# We May Find A Partial Evidence of Ideological Shirking in Selected Bills
# 3-2) For Significant Bills: L2, L15, L37
# Voting Yea Models -with ideological difference from Floor Median*
# Lame Duck multiplied by Ideological Drift from the floor median 
# With Control Variables 

# logit Model for Legislation 2 voting Yea only
  
fit.5a <- glm(leg2_yeav ~ return19 + ideo_d1 + return19:ideo_d1 + X_dum1 + 
                seniority + district1, data=data2, family=binomial(link="logit")) 

summary(fit.5a)

fit.5b <- glm(leg2_yeav ~ return19 + ideo_d1 + ideoshirk + X_dum1 + 
                seniority + district1, data=data2, family=binomial(link="logit")) 

summary(fit.5b)

# logit model for Legislation 15 voting Yea only
fit.6a <- glm(leg15_yeav ~ return19 + ideo_d1 + return19:ideo_d1 + 
                X_dum2 + seniority + district2, data=data2,
              family=binomial(link="logit"))
summary(fit.6a)

fit.6b <- glm(leg15_yeav ~ return19 + ideo_d1 + ideoshirk + 
                X_dum2 + seniority + district2, data=data2,
              family=binomial(link="logit"))
summary(fit.6b)

# logit model for Legislation 37 voting Yea only
fit.7a <- glm(leg37_yeav ~ return19 + ideo_d1 + return19:ideo_d1 + 
                X_dum2 + seniority + district2 + job, data=data2,
              family=binomial(link="logit"))
summary(fit.7a)

fit.7b <- glm(leg37_yeav ~ return19 + ideo_d1 + ideoshirk + 
                X_dum2 + seniority + district2 + job, data=data2,
              family=binomial(link="logit"))
summary(fit.7b)

# creating a basic graph of the conditional coefficients
interplot(m = fit.5a, var1 = "return19", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("19대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

interplot(m = fit.6a, var1 = "return19", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("19대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

interplot(m = fit.7a, var1 = "return19", var2 = "ideo_d1",
          point=FALSE, hist=TRUE,ralpha = 0.3) +
  geom_hline(yintercept = 0, color="green", linetype = "dashed") +
  xlab("이념 중앙값과의 차이의 절대값") +
  ylab("19대 당선자 투표참여 계수 추정치") +
  theme_bw() +
  # changing the angle of x labels for a clearer vision
  theme(text=element_text(size=10, family="NanumBarunGothic YetHangul"),
        plot.title = element_text(size=15,family="NanumBarunGothic YetHangul", 
                                  colour="black"),
        axis.text.x  = element_text(angle=90))

library(effects)

plot(effect("return19",fit.7a, rescale.axis=FALSE,
            xlevel=list(c("Leave","Return"))),
     band.colors="blue", band.transparency=.15,
     ylab="Prob(Leg15 Yea)",
     main="Effect of Return")


plot(effect("seniority",fit.7a, rescale.axis=TRUE),
     band.colors="blue", band.transparency=.15,
     ylab="Prob(Leg15 Yea)",
     main="Conditional Effect of Seniority")

plot(effect("ideo_d1",fit.7a, rescale.axis=FALSE,
            scales=list(y=seq(0,1,by=0.2))),
     band.colors="blue", band.transparency=.15,
     ylab="Prob(Leg15 Yea)",
     xlab="Ideological Distance from the Floor Median",
     main="Conditional Effect")

plot(allEffects(fit.7b,rescale.axis=F),
     band.colors="blue", band.transparency=.15,
     ylab="Prob(Leg15 Yea)")

source("interaction_plots.R")

############################################################################ 
# 17th lame-duck 
############################################################################ 

par(mar=c(5,6,4,4))
interaction_plot_continuous(model=fit.1b, effect = "return18Return",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance")

interaction_plot_continuous(model=fit.2b, effect = "X_18th",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance")

interaction_plot_continuous(model=fit.3b, effect = "X_18th",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance")

interaction_plot_continuous(model=fit.4b, effect = "X_18th",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance")

par(mar=c(5,4,4,2))

############################################################################ 
# 18th lame-duck 
############################################################################ 

par(mar=c(5,6,4,4), mfrow=c(2,2))
interaction_plot_continuous(model=fit.5b, effect = "return19yes",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance (No.2)")

interaction_plot_continuous(model=fit.6b, effect = "return19yes",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance (No.15)")

interaction_plot_continuous(model=fit.7b, effect = "return19yes",
                            moderator = "ideo_d1",
                            interaction = "ideoshirk",
                            median = TRUE,
                            breaks=15,
                            xlabel = "Ideological Distance from the Floor",
                            ylabel = "Marginal effect of Returning Member \n on Yea Vote",
                            title = "Relationship between Returning Member and Yea Vote \n for Different Levels of Ideological Distance (No.37)")

par(mar=c(5,4,4,2), mfrow=c(1,1))





